package gui.data.others;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

import calcs.ConverterCalculator;

public class ConverterGui extends JPanel implements ActionListener{
	
	JTextField input = new JTextField();
	JLabel output = new JLabel("", JLabel.CENTER);

	ConverterCalculator cal = new ConverterCalculator(input, output);
	
	JComboBox<String> selector1;
	JComboBox<String> selector2;
	String[] current;
	
	String[] temps = { "Celcius", "Fahrenheit", "Kelvin", "Rankine"};
	
	String[] weights = { "Kilograms", "Pounds", "Stones", "Grams",
			"Ounces", "Carats" };
	
	String[] lengths = { "Miles", "Kilometers", "Meters", "Yards",
			"Feet", "Inches", "Centimeters" };
	
	String[] areas = { "Acres", "Hectars", "Square feet", "Square meters",
			"Square miles", "Square inches", "Square kilometers" };
	
	String[] volumes = { "Imperial gallons", "Liters", "Imperial pints", "Barrels",
			"Milliliters", "Imperial ounces" };
	
	public ConverterGui(){
		
		this.setLayout(new GridLayout(6, 2));
		this.setPreferredSize(new Dimension(500, 500));
		
		ButtonGroup group = new ButtonGroup();

		JRadioButton temp = new JRadioButton("Temperature");
		temp.setActionCommand("temp");
		temp.addActionListener(this);
		
		JRadioButton weight = new JRadioButton("Weight");
		weight.setActionCommand("weight");
		weight.addActionListener(this);

		JRadioButton length = new JRadioButton("Length");
		length.setActionCommand("length");
		length.addActionListener(this);
		
		JRadioButton area = new JRadioButton("Area");
		area.setActionCommand("area");
		area.addActionListener(this);
		
		JRadioButton volume = new JRadioButton("Volume");
		volume.setActionCommand("volume");
		volume.addActionListener(this);

		group.add(volume);
		group.add(area);
		group.add(length);
		group.add(weight);
		group.add(temp);
		
		temp.setSelected(true);
		
		selector1 = new JComboBox<String>(temps);
		selector2 = new JComboBox<String>(temps);
		selector1.setSelectedIndex(0);
		selector2.setSelectedIndex(0);
		current = temps;
		selector1.addItemListener(new ItemListener(){
			@Override
			public void itemStateChanged(ItemEvent e) {
				cal.setFrom(findFromArray(""+e.getItem()));
			}
			
		});
		selector2.addItemListener(new ItemListener(){
			@Override
			public void itemStateChanged(ItemEvent e) {
				cal.setTo(findFromArray(""+e.getItem()));
			}
			
		});
		
		this.add(temp);
		this.add(weight);
		this.add(length);
		this.add(area);
		this.add(volume);
		
		this.add(new JLabel());
		
		this.add(selector1);
		this.add(selector2);
		
		this.add(input);
		this.add(output);
		this.add(new JLabel());
		
		JButton calculate = new JButton("Calculate");
		calculate.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseReleased(MouseEvent e) {
				cal.run();
			}
		});
		
		this.add(calculate);
	}
	
	public int findFromArray(String s){
		int count = 0;
		for(String a:current){
			count++;
			if(s.equals(a)){
				break;
			}
		}
		return count;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getActionCommand().equals("temp")){
			current = temps;
			selector1.setModel(new JComboBox<String>(temps).getModel());
			selector2.setModel(new JComboBox<String>(temps).getModel());
			selector1.setSelectedIndex(0);
			selector2.setSelectedIndex(0);
			cal.setGuide(1);
		}else if (e.getActionCommand().equals("weight")){
			current = weights;
			selector1.setModel(new JComboBox<String>(weights).getModel());
			selector2.setModel(new JComboBox<String>(weights).getModel());
			selector1.setSelectedIndex(0);
			selector2.setSelectedIndex(0);
			cal.setGuide(2);
		}else if (e.getActionCommand().equals("length")){
			current = lengths;
			selector1.setModel(new JComboBox<String>(lengths).getModel());
			selector2.setModel(new JComboBox<String>(lengths).getModel());
			selector1.setSelectedIndex(0);
			selector2.setSelectedIndex(0);
			cal.setGuide(3);
		}else if (e.getActionCommand().equals("area")){
			current = areas;
			selector1.setModel(new JComboBox<String>(areas).getModel());
			selector2.setModel(new JComboBox<String>(areas).getModel());
			selector1.setSelectedIndex(0);
			selector2.setSelectedIndex(0);
			cal.setGuide(4);
		}else if (e.getActionCommand().equals("volume")){
			current = volumes;
			selector1.setModel(new JComboBox<String>(volumes).getModel());
			selector2.setModel(new JComboBox<String>(volumes).getModel());
			selector1.setSelectedIndex(0);
			selector2.setSelectedIndex(0);
			cal.setGuide(5);
		}
	}

	public void setOutput(final String string) {
		SwingUtilities.invokeLater(new Runnable() {
		      @Override
		      public void run() {
		    	  output.setText(string);
		      }
		   });
	}
}
